package com.controller;

import com.mypg.Info;
import com.service.InfoService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class InfoController {

    @Autowired
    InfoService infoService;
    String MyFilePath="E:\\毕设有关\\上传的文件\\资讯有关";

    @RequestMapping("/info_table_init.do")
    @ResponseBody
    public List<Info> doInfo_table_init(@RequestParam(defaultValue = "0")int start_row,
                                        @RequestParam(defaultValue = "5")int page_nums) throws ParseException {

        return changeRows("","","",start_row,page_nums,0);
    }

    @RequestMapping("/info_table_split.do")
    @ResponseBody
    public List<Info> doInfo_table_split(String info_class,String time_start,String time_end,
                                         @RequestParam(defaultValue = "0")int start_row,
                                         @RequestParam(defaultValue = "5")int page_nums) throws ParseException {
        return changeRows(info_class,time_start,time_end,start_row,page_nums,1);
    }

    @RequestMapping("/info_table_add_with_file.do")
    @ResponseBody
    public Map<String,Object> doInfo_table_add_with_file(String info_title, String info_class,
                                                         @RequestParam(value="file",required = false) MultipartFile file){

        Info info=new Info();
        String info_content="";
        Timestamp timestamp=new Timestamp(System.currentTimeMillis());
        BufferedReader bufferedReader = null;
        if(!file.isEmpty()){
            try {
                InputStream in = file.getInputStream();
                bufferedReader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
//                char[] charBuffer = new char[128];
                int a = 0;
                while((a = bufferedReader.read())!=-1){
                    char ch=(char)a;
                    info_content+=ch;
                }
                in.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        info.setInfo_title(info_title);
        info.setInfo_content(info_content);
        info.setInfo_class(info_class);
        info.setInfo_time(timestamp);
        infoService.insertInfo(info);
        HashMap<String,Object> map=new HashMap<>();
        map.put("str",info_content);
        return map;

    }
    @RequestMapping("/info_table_add_no_file.do")
    @ResponseBody
    public Map<String, Object> doInfo_table_add_no_file(Info info){
        Timestamp timestamp=new Timestamp(System.currentTimeMillis());
        info.setInfo_time(timestamp);
        infoService.insertInfo(info);
        HashMap<String,Object> map=new HashMap<>();
        map.put("str",info.getInfo_content());
        return map;
    }

    @RequestMapping("/info_table_delete.do")
    @ResponseBody
    public List<Info> doInfo_table_delete(int info_id,String info_class,String time_start,String time_end,
                                          @RequestParam(defaultValue = "0")int start_row,
                                          @RequestParam(defaultValue = "5")int page_nums) throws ParseException {
        infoService.deleteInfoByinfo_id(info_id);
        return changeRows(info_class,time_start,time_end,start_row,page_nums,1);
    }

    @RequestMapping("/info_table_delete_many.do")
    @ResponseBody
    public List<Info> doInfo_table_delete_many(String[] info_ids,String info_class,String time_start,String time_end,
                                               @RequestParam(defaultValue = "0")int start_row,
                                               @RequestParam(defaultValue = "5")int page_nums) throws ParseException {
        info_ids[0]=info_ids[0].replace("[","");
        info_ids[info_ids.length-1]=info_ids[info_ids.length-1].replace("]","");
        for(String info_id:info_ids){
            info_id=info_id.replace("\"","");
            if(!"-1".equals(info_id))
                infoService.deleteInfoByinfo_id(Integer.parseInt(info_id));
        }
        return changeRows(info_class,time_start,time_end,start_row,page_nums,1);
    }

    @RequestMapping("/info_table_edit_init.do")
    @ResponseBody
    public Info doInfo_table_edit_init(int info_id){
        return infoService.selectInfoByinfo_id(info_id);
    }

    @RequestMapping("/info_table_edit_no_file.do")
    @ResponseBody
    public Map<String,Object> doInfo_table_edit_no_file(Info info){
        infoService.updateInfoByinfo_id(info);
        HashMap<String,Object> map=new HashMap<>();
        map.put("str",info.getInfo_content());
        return map;
    }

    @RequestMapping("/info_table_edit_with_file.do")
    @ResponseBody
    public Map<String, Object> doInfo_table_edit_with_file(int info_id, String info_title, String info_class,
                                                           @RequestParam(value="file",required = false) MultipartFile file){

        Info info=new Info();
        String info_content="";
        BufferedReader bufferedReader = null;
        if(!file.isEmpty()){
            try {
                InputStream in = file.getInputStream();
                bufferedReader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
//                char[] charBuffer = new char[128];
                int a = 0;
                while((a = bufferedReader.read())!=-1){
                    char ch=(char)a;
                    info_content+=ch;
                }
                in.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        info.setInfo_id(info_id);
        info.setInfo_title(info_title);
        info.setInfo_content(info_content);
        info.setInfo_class(info_class);
        infoService.updateInfoByinfo_id(info);
        HashMap<String,Object> map=new HashMap<>();
        map.put("str",info_content);
        return map;
    }






























    public List<Info> changeRows(String info_class,String time_start,String time_end,
                                 int start_row,int page_nums,int flag) throws ParseException {
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Timestamp timestamp_start=null;
        Timestamp timestamp_end=null;
        if(time_start!=null&&time_end!=null) {
            if ("".equals(time_start)) {
                time_start = "1970-01-01 01:01";
                timestamp_start = new Timestamp(df.parse(time_start).getTime());
            } else {
                time_start = time_start.replace("T", " ");
                timestamp_start = new Timestamp(df.parse(time_start).getTime());
            }
            if ("".equals(time_end)) {
                timestamp_end = new Timestamp(System.currentTimeMillis());
            } else {
                time_end = time_end.replace("T", " ");
                timestamp_end = new Timestamp(df.parse(time_end).getTime());
            }
        }

        List<Info> infoList = infoService.selectInfoForinfo_table_jsp(info_class,timestamp_start,timestamp_end,start_row,page_nums);
        Info info=new Info();
        if(flag==0)
            info.setInfo_id(infoService.selectInfo_count());
        else
            info.setInfo_id(infoService.selectInfo_count_plus(info_class,timestamp_start,timestamp_end));
        infoList.add(info);
        return infoList;
    }
}
